<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>29$watch的方法</title>
</head>
<body>
<div id="app">
    <input type="text" v-model="worker.age">{{ worker.age }}<br>
    <input type="text" v-model="user">{{ user }}<br>
</div>
<script src="../vue.global.3.0.5.js"></script>
<script>
    const outerAssembly = {
        data() {
            return {
                
            }
        },
        template: `
        <input type="text" value="Mr.GuoZi" ref="userZi" />
        `
    }

    //1、创建一个Vue应用实例，本身是最顶层的根组件
    const app = Vue.createApp({
        data() {
            return { 
                user: " Mr.Lee",
                worker: {
                    name: "Mr.Guo",
                    age: 15
                }
            }
        },
        created() {
            //【$watch写法1】
            this.$watch("user", (newValue, oldValue)=>{
                console.log("created()内部-简单属性：newValue="+newValue+", oldValue="+oldValue);
            }),
            //【$watch写法2】
            this.$watch(()=>this.worker.age, (newValue, oldValue)=>{
                console.log("created()内部-对象的属性：newValue="+newValue+", oldValue="+oldValue);
            }),
            //【$watch写法3】
            this.$watch("worker", (newValue, oldValue)=>{
                console.log("created()内部-对象：newValue.age="+newValue.age+", oldValue.age="+oldValue.age);
            },{
                deep: true
            })
        },
        computed: {
            
        },
        watch: {

        },
        components: {
            //命名规则必须是小写字母加-
            "outer-assembly": outerAssembly
        }
    });
    //2、定义一个全局组件
    app.component('button-counter', {
        //数据
        data() {
            return {
                counter: 100
            }
        },
        //模板
        template: `
            计算器: {{counter}}
            <button v-on:click="counter++">counter++</button><br>
        `
    });
    //3、挂载，返回值是代理对象并不是应用实例本身
    const vm = app.mount("#app");
    //【$watch写法4】
    const unwatch = vm.$watch("user", (newValue, oldValue)=>{
        console.log("created()外部-简单属性：newValue="+newValue+", oldValue="+oldValue);
    })
    //取消监听
    unwatch();
</script>
</body>
</html>